package com.youku.plugins.interaction.view
{
   import flash.utils.Dictionary;
   import com.youku.interfaces.IRootData;
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.PluginEventDispather;
   import com.youku.plugins.interaction.model.InteractionDataProxy;
   import com.youku.plugins.interaction.model.data.InteractionList;
   import com.youku.interfaces.IPlayListData;
   import com.youku.PlayerConfig;
   import com.youku.events.ConductorEvent;
   import com.youku.events.PlayerControlEvent;
   import com.youku.plugins.interaction.events.InteractionEvent;
   import com.youku.events.PluginEvent;
   import com.youku.plugins.interaction.model.data.InteractionSwfList;
   import com.youku.conductor.ConductorState;
   import com.youku.plugins.interaction.model.data.InteractionSwfData;
   import com.youku.plugins.interaction.InteractionConstant;
   import flash.display.DisplayObject;
   import flash.events.Event;
   import flash.net.sendToURL;
   import flash.net.URLRequest;
   
   public class InteractionMediator extends Object
   {
      
      public function InteractionMediator(param1:MovieClip, param2:IPlayerProxy, param3:PluginEventDispather, param4:InteractionDataProxy) {
         super();
         this._mainMc = param1;
         this._playerProxy = param2;
         this._eventDispathcer = param3;
         this._dataProxy = param4;
         this._swfDictionary = new Dictionary();
         this._typeArr = [];
         this._swfDataArray = [];
         this._rootData = PlayerConfig.rootData;
         this._interactionList = InteractionList.getInstance();
         this.init();
      }
      
      private var _swfDictionary:Dictionary;
      
      private var _typeArr:Array;
      
      private var _swfDataArray:Array;
      
      private var _rootData:IRootData;
      
      private var _mainMc:MovieClip;
      
      private var _thisMc:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _eventDispathcer:PluginEventDispather;
      
      private var _dataProxy:InteractionDataProxy;
      
      private var _interactionList:InteractionList;
      
      private function get playlistData() : IPlayListData {
         return PlayerConfig.playListData;
      }
      
      private function init() : void {
         this._thisMc = new MovieClip();
         this._mainMc.addChild(this._thisMc);
         this._playerProxy.addEventListener(ConductorEvent.METADATA_UPDATED,this.onMetaDataUpdated);
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onStateChanged);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_START_TO_PLAY,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_RESET,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.COMMON_ERROR,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.DO_INTERACT,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.ADD_INTERACT,this.onPlayerControlEvent);
         this._interactionList.addEventListener(InteractionEvent.SWF_LOAD_COMPLETE,this.onSwfLoaded);
         this._interactionList.addEventListener(InteractionEvent.SWF_LOAD_ERROR,this.onSwfLoadError);
         this._eventDispathcer.addEventListener(InteractionEvent.INTERACTION_DATA_COMPLETE,this.onInteractionEvent);
         this._eventDispathcer.addEventListener(InteractionEvent.INTERACTION_DATA_ERROR,this.onInteractionEvent);
      }
      
      private function onShowInteraction(param1:PluginEvent) : void {
      }
      
      private function onInteractionEvent(param1:InteractionEvent) : void {
         switch(param1.type)
         {
            case InteractionEvent.INTERACTION_DATA_COMPLETE:
               this.addSwfs(param1.data as InteractionSwfList);
               break;
            case InteractionEvent.INTERACTION_DATA_ERROR:
               break;
         }
      }
      
      private function onMetaDataUpdated(param1:ConductorEvent) : void {
      }
      
      private function onStateChanged(param1:ConductorEvent) : void {
         var _loc2_:String = param1.data as String;
         switch(_loc2_)
         {
            case ConductorState.VIDEO:
               this.show();
               break;
            case ConductorState.INITIALIZE:
            case ConductorState.PRE_VIDEO:
            case ConductorState.POST_VIDEO:
            case ConductorState.AD:
               this.hide();
               break;
            case ConductorState.VIDEO_END:
            case ConductorState.ERROR:
               this.hide();
               break;
         }
      }
      
      private function hide() : void {
         var _loc3_:InteractionItem = null;
         var _loc1_:int = this._typeArr.length;
         var _loc2_:* = 0;
         while(_loc2_ < _loc1_)
         {
            _loc3_ = this._swfDictionary[this._typeArr.pop()];
            if(_loc3_)
            {
               _loc3_.hide();
            }
            _loc2_++;
         }
      }
      
      private function show() : void {
         var _loc3_:InteractionItem = null;
         var _loc1_:int = this._typeArr.length;
         var _loc2_:* = 0;
         while(_loc2_ < _loc1_)
         {
            _loc3_ = this._swfDictionary[this._typeArr.pop()];
            if(_loc3_)
            {
               _loc3_.visible = true;
            }
            _loc2_++;
         }
      }
      
      private function onPlayerControlEvent(param1:PlayerControlEvent) : void {
         var _loc2_:* = 0;
         var _loc3_:IPlayListData = null;
         var _loc4_:InteractionSwfData = null;
         var _loc5_:InteractionItem = null;
         var _loc6_:* = 0;
         var _loc7_:InteractionItem = null;
         switch(param1.type)
         {
            case PlayerControlEvent.COMMON_ERROR:
            case PlayerControlEvent.COMMON_RESET:
               _loc2_ = this._typeArr.length;
               _loc6_ = 0;
               while(_loc6_ < _loc2_)
               {
                  _loc7_ = this._swfDictionary[this._typeArr.pop()];
                  if(_loc7_)
                  {
                     _loc7_.dispose();
                  }
                  _loc6_++;
               }
               this._typeArr = [];
               this._swfDictionary = new Dictionary();
               this._interactionList.dispose();
               break;
            case PlayerControlEvent.DO_INTERACT:
               this.jsCallAs(param1.data.type,param1.data);
               break;
            case PlayerControlEvent.COMMON_START_TO_PLAY:
               _loc3_ = PlayerConfig.playListData;
               if(_loc3_.interact)
               {
                  this._dataProxy.loadData(this._playerProxy);
               }
               break;
            case PlayerControlEvent.ADD_INTERACT:
               _loc4_ = new InteractionSwfData();
               _loc4_.name = param1.data.name;
               if(!_loc4_.name)
               {
                  return;
               }
               if(this._swfDictionary[_loc4_.name])
               {
                  return;
               }
               if(_loc4_.name == InteractionConstant.XINGMENG_PLUGIN)
               {
                  _loc4_.url = this._playerProxy.rootGetData.rootBaseURL + "/" + InteractionConstant.XINGMENG_PLUGIN_URL;
                  if((param1.data.info) && param1.data.info is String)
                  {
                     _loc4_.url = _loc4_.url + ("?" + param1.data.info);
                  }
                  _loc5_ = new InteractionItem(this._thisMc,_loc4_);
               }
               if(_loc5_)
               {
                  this._swfDictionary[_loc4_.name] = _loc5_;
                  this._typeArr.push(_loc4_.name);
                  this._interactionList.addToList(_loc4_.url,_loc4_.name);
                  this._interactionList.load();
               }
               break;
         }
      }
      
      private function onSwfLoaded(param1:InteractionEvent) : void {
         var _loc4_:DisplayObject = null;
         var _loc2_:InteractionItem = this._swfDictionary[param1.data.type];
         if(!_loc2_)
         {
            return;
         }
         var _loc3_:Object = {};
         _loc3_.type = param1.data.type;
         _loc3_.wintype = this._playerProxy.rootGetData.rootWinType;
         _loc3_.vid = this._playerProxy.playerData.videoIdEncode;
         _loc3_.href = this._playerProxy.playerData.href;
         _loc3_.Pf = this._rootData.pf;
         _loc3_.test_domain = _loc2_.swfData.test_domain;
         _loc3_.sid = this.playlistData.sid;
         if(this.playlistData.show)
         {
            _loc3_.showID = this.playlistData.show.showID;
         }
         if(this.playlistData.userId)
         {
            _loc3_.owner_id = this.playlistData.userId;
         }
         if(PlayerConfig.isTudouPlayer)
         {
            _loc3_.Sc = 2;
         }
         else
         {
            _loc3_.Sc = 1;
         }
         if(!(this.playlistData.verifyUser == null) && (this.playlistData.verifyUser.isHaveData))
         {
            _loc3_.verify_user = 1;
            _loc3_.verifyUserData = this.playlistData.verifyUser;
         }
         else
         {
            _loc3_.verify_user = 0;
         }
         if(_loc2_)
         {
            _loc4_ = param1.data.item as DisplayObject;
            _loc4_.addEventListener("interactToAddPoints",this.onInteractToAddPoints);
            _loc2_.addItem(_loc4_,_loc3_);
         }
         this.sendReport(param1.data.type,0);
      }
      
      private function onInteractToAddPoints(param1:Event) : void {
         var data:Array = null;
         var e:Event = param1;
         try
         {
            data = (e as Object).data as Array;
         }
         catch(e:Error)
         {
            data = [];
         }
         this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.INTERACT_INTERACTPOINT_LOADED,data));
      }
      
      private function onSwfLoadError(param1:InteractionEvent) : void {
         var _loc2_:InteractionItem = this._swfDictionary[param1.data.type];
         if(_loc2_)
         {
            _loc2_.dispose();
         }
         this._swfDictionary[param1.data.type] = null;
         this.sendReport(param1.data.type,param1.data.errorCode);
      }
      
      private function addSwfs(param1:InteractionSwfList) : void {
         var _loc3_:InteractionSwfData = null;
         var _loc4_:InteractionItem = null;
         this._swfDataArray = param1.interactionSwfs;
         var _loc2_:* = 0;
         while(_loc2_ < this._swfDataArray.length)
         {
            _loc3_ = this._swfDataArray[_loc2_] as InteractionSwfData;
            _loc4_ = new InteractionItem(this._thisMc,_loc3_);
            this._swfDictionary[_loc3_.name] = _loc4_;
            this._typeArr.push(_loc3_.name);
            this._interactionList.addToList(_loc3_.url,_loc3_.name);
            _loc2_++;
         }
         this._interactionList.load();
      }
      
      private function jsCallAs(param1:String, param2:Object) : void {
         if(this._swfDictionary[param1])
         {
            this._swfDictionary[param1].jsCallAs(param2);
         }
      }
      
      private function sendReport(param1:String, param2:int) : void {
         var _loc3_:* = "http://p.l.youku.com/interact?";
         _loc3_ = _loc3_ + ("vvid=" + this.playlistData.sid);
         if(this.playlistData.show)
         {
            _loc3_ = _loc3_ + ("&sid=" + this.playlistData.show.showID);
         }
         _loc3_ = _loc3_ + ("&vid=" + this._playerProxy.playerData.videoIdEncode);
         _loc3_ = _loc3_ + ("&type=" + "swf_load");
         _loc3_ = _loc3_ + ("&tcode=" + param1);
         _loc3_ = _loc3_ + ("&full=" + this.getFullscreenNumber());
         _loc3_ = _loc3_ + ("&wintype=" + this.getWinTypeNumber());
         _loc3_ = _loc3_ + ("&error=" + param2);
         sendToURL(new URLRequest(_loc3_));
      }
      
      private function getFullscreenNumber() : Number {
         var _loc1_:Number = 0;
         if(this._playerProxy.playerData.isInFullScreen)
         {
            _loc1_ = 1;
         }
         else
         {
            _loc1_ = 0;
         }
         return _loc1_;
      }
      
      private function getWinTypeNumber() : Number {
         var _loc1_:String = this._playerProxy.rootGetData.rootWinType;
         var _loc2_:Number = 0;
         if(_loc1_ == "index")
         {
            _loc2_ = 0;
         }
         else if(_loc1_ == "interior")
         {
            _loc2_ = 1;
         }
         else if(_loc1_ == "adshow")
         {
            _loc2_ = 2;
         }
         else if(_loc1_ == "BDskin")
         {
            _loc2_ = 3;
         }
         else if(_loc1_ == "exterior")
         {
            _loc2_ = 4;
         }
         else if(_loc1_ == "touch")
         {
            _loc2_ = 10;
         }
         else if(_loc1_ == "tudoupartner")
         {
            _loc2_ = 20;
         }
         else if(_loc1_ == "ikupartner")
         {
            _loc2_ = 21;
         }
         
         
         
         
         
         
         
         return _loc2_;
      }
   }
}
